/**
 * 
 */
package com.hb.webapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hb.Constants;
import com.hb.model.Category;
import com.hb.model.Job;
import com.hb.service.CategoryManager;
import com.hb.service.JobManager;
import com.hb.util.DateUtil;
import com.hb.webapp.util.ConvertUtil;

/**
 * @author Pham Hong Hung
 *
 */
@Controller
public class JobSearchController extends BaseFormController {

	private CategoryManager categoryManager;
	
	private JobManager jobManager;
	
	@Autowired
	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}
	@Autowired
	public void setJobManager(JobManager jobManager) {
		this.jobManager = jobManager;
	}

	@ModelAttribute
    @RequestMapping("/jobsearch")
    protected ModelAndView showForm(HttpServletRequest request) throws Exception {
		List<Job> list = getJobs(request);
		ModelAndView mav = new ModelAndView();
		mav.addObject(Constants.LIST, list);
		return mav;
    }
	@ModelAttribute
	@RequestMapping("/jobList")
    protected ModelAndView search(HttpServletRequest request) throws Exception {
		List<Job> list = getJobs(request);
		ModelAndView mav = new ModelAndView("jobList");
		mav.addObject(Constants.LIST, list);
		
        return mav;
    }
	
	@ModelAttribute
	@RequestMapping("/getAllCategory")
    protected void getAllCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Category> categories = this.categoryManager.getAll();
		ServletOutputStream out = response.getOutputStream();
		out.write(ConvertUtil.convertCategories2Options(categories).getBytes());
		out.flush();
    }
	
	private List<Job> getJobs(HttpServletRequest request) throws Exception{
		
		String jobName = request.getParameter("jobName");
		String categoryStr = request.getParameter("category");
		String fromDateStr = request.getParameter("fromDate");
		String toDateStr = request.getParameter("toDate");
		String fromFeeStr = request.getParameter("fromFee");
		String toFeeStr = request.getParameter("toFee");
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(!StringUtils.isBlank(jobName)){
			map.put(Constants.JOB_NAME, new String("%" + jobName + "%"));
		}
		if(!StringUtils.isBlank(categoryStr)){
			map.put(Constants.CATEGORY, new Long(categoryStr));
		}
		if(!StringUtils.isBlank(fromDateStr)){
			map.put(Constants.FROM_DATE, DateUtil.convertStringToDate(DateUtil.dd_Slash_MM_Slash_yyyy, fromDateStr));
		}
		if(!StringUtils.isBlank(toDateStr)){
			map.put(Constants.TO_DATE, DateUtil.convertStringToDate(DateUtil.dd_Slash_MM_Slash_yyyy, toDateStr));
		}
		if(!StringUtils.isBlank(fromFeeStr)){
			map.put(Constants.FROM_FEE, new Long(fromFeeStr));
		}
		if(!StringUtils.isBlank(toFeeStr)){
			map.put(Constants.TO_FEE, new Long(toFeeStr));
		}
		
		List<Job> list = this.jobManager.getJobs(map);
		
		return list;
	}
	
}
